﻿using UnityEngine;
using UnityEditor;
using System.Collections;

public class BaseEditor : Editor
{
	public class SceneTile
	{
		public enum Type
		{
			None,
			Add,
			Remove,
		}

		public Tile tile;
		public Type type;
		public bool click;
	}

	protected static SceneTile sceneTile = new SceneTile();

	private static bool ctrlDown = false;	// Remove Tile
	private static bool shftDown = false;	// Add Tile

	// Call this function in OnSceneGUI
	protected static void UpdateSceneTile()
	{
		shftDown = Event.current.shift;
		ctrlDown = Event.current.control;

		if (shftDown)
		{
			sceneTile.type = SceneTile.Type.Add;
		}
		else if (ctrlDown)
		{
			sceneTile.type = SceneTile.Type.Remove;
		}
		else
		{
			sceneTile.type = SceneTile.Type.None;
		}

		Vector3 tilePosition = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition).origin;
		tilePosition.x = (int)(tilePosition.x + 0.5f);
		tilePosition.y = (int)(tilePosition.y + 0.5f);

		Collider2D col = Physics2D.OverlapPoint(tilePosition, (1 << LayerMask.NameToLayer("Tile")));
		sceneTile.tile = col == null ? null : col.GetComponent<Tile>();

		if (Event.current.type == EventType.MouseDown && Event.current.button == 1 && sceneTile.click == false)
		{
			sceneTile.click = true;
		}
		else if (Event.current.type == EventType.MouseUp && Event.current.button == 1 && sceneTile.click == true)
		{
			sceneTile.click = false;
		}
	}

	protected void Instructions()
	{
		EditorGUILayout.BeginVertical("box");
		{
			GUILayout.Label("TO EDIT", EditorStyles.boldLabel);
			GUILayout.Label("Press ctrl and right-click to remove tiles");
			GUILayout.Label("Press shift and right-click to add tiles");
		}
		EditorGUILayout.EndVertical();
	}

	public enum NeighbourState
	{
		Normal,
		Add,
		Remove
	}

	/*protected static void DrawNeighbour(Vector3 _self, Vector3 _neighbour, NeighbourState _state)
	{
		float rightDot = Vector3.Dot(_self - _neighbour, Vector3.right);
		float upDot = Vector3.Dot(_self - _neighbour, Vector3.up);

		string direction = "";
		if (rightDot == 1 && upDot == 0)
		{
			direction = "Left";
		}
		else if (rightDot == -1 && upDot == 0)
		{
			direction = "Right";
		}
		else if (rightDot == 0 && upDot == -1)
		{
			direction = "Up";
		}
		else if (rightDot == 0 && upDot == 1)
		{
			direction = "Down";
		}
		else
		{
			direction = "NeighbourMissing";
		}
		
		string state = "";
		if (direction != "NeighbourMissing")
		{
			switch (_state)
			{
			case NeighbourState.Normal: state = "Select"; break;
			case NeighbourState.Add: state = "Add"; break;
			case NeighbourState.Remove: state = "Remove"; break;
			}
		}

		Gizmos.DrawIcon(_self, "gizmo" + state + direction);
	}*/
	
	protected void DrawSquare(Vector3 _position, Color _color)
	{
		Handles.color = _color;
		Handles.DotCap(0, _position, Quaternion.identity, 0.1f);
	}
	
	protected void DrawCircle(Vector3 _position, Color _color)
	{
		Handles.color = _color;
		Handles.ArrowCap(0, _position, Quaternion.identity, 1.5f);
	}

	protected void DrawLineCircle(Vector3 _start, Vector3 _end, Color _color)
	{
		Handles.color = _color;
		Handles.DrawLine(_start, _end);
		Handles.ArrowCap(0, _end, Quaternion.identity, 1.5f);
	}
}
